import { BaseCommand } from '@amcax/base';
import { useEffect, useRef } from 'react';
import { CenterLineRabbetCommandProps } from './types';
import CmdInteractor from './CmdInteractor';

function CenterLineRabbetCommand({
  onClose,
  mode,
}: CenterLineRabbetCommandProps) {
  const cmdRef = useRef<CmdInteractor>(null);
  useEffect(() => {
    cmdRef.current = new CmdInteractor(mode);
    cmdRef.current.addExit(onClose);
    return () => {
      cmdRef.current?.destroy();
      cmdRef.current = null;
    };
  }, [mode]);
  return <></>;
}

export const cmdCenterLineRabbet: BaseCommand = {
  id: 'CmdCenterLineRabbet',
  label: 'command.center-line-rabbet',
  icon: 'commands/sketch/center-line-rabbet',
  group: 'sketch',
  create: function (_, onClose: () => void) {
    return (
      <CenterLineRabbetCommand
        onClose={onClose}
        mode='center-line'
      />
    );
  },
};

export const cmdCenterPointCenterLineRabbet: BaseCommand = {
  id: 'CmdCenterPointCenterLineRabbet',
  label: 'command.center-point-center-line-rabbet',
  icon: 'commands/sketch/center-point-center-line-rabbet',
  group: 'sketch',
  create: function (_, onClose: () => void) {
    return (
      <CenterLineRabbetCommand
        onClose={onClose}
        mode='mid-point'
      />
    );
  },
};
